<?php
class Z_Application 
{
	
	private static $_application = null;
	private $_applicationRoot = '';
	private $_zend_config = null;
	private $_zend_autoloader = null;
	private  function __construct()
	{
		require_once 'Zend/Loader/Autoloader.php';
		$_zend_autoloader = Zend_Loader_Autoloader::getInstance();
		$this->_zend_autoloader = $_zend_autoloader;
	}
	public static function Booting()
	{
		if(self::$_application == null)
			return self::$_application = new self();
		else 
			return self::$_application;
	}
	public function setApplicationRoot($applicationRoot)
	{
		$this->_applicationRoot = $applicationRoot;
		return $this;
	}
	public function getApplicationRoot()
	{
		if(empty($this->_applicationRoot))
		{
			 //  throw new Z_Exception('No default applicationRoot');
			 echo "not";
		}
		else
		{
			return $this->_applicationRoot;
		}
	}
	public function setConfigPath($configFile, $section = null)
	{
		$configPath = $this->getApplicationRoot().'/'.$configFile;
		if(file_exists($configPath))
		{
			$pathInfo = pathinfo($configPath);
			$extension = ucwords($pathInfo["extension"]);
			if($extension == 'Ini' || $extension == 'Xml')
			{
				$ConfigClass = 'Zend_Config_'.$extension;
				$this->_zend_config = new $ConfigClass($configPath, $section);
			}
			else 
			{
				$this->_zend_config = new Zend_Config(require $configPath);
			}
		}
	
		return $this;
	}
	public function getConfig()
	{
		return $this->_zend_config;
	}
	public function Autoload()
	{
		$_zend_config = $this->getConfig();
		$applicationRoot = $this->getApplicationRoot();
		$Z_ROOT = dirname(__FILE__);
		if(!file_exists($applicationRoot.'/'.$_zend_config->environment->autoload))
		{
			//$includePaths[] = $Z_ROOT;
			include $Z_ROOT.'/C/GeneratorAssembly.php';
			$classdirections = $_zend_config->application->classdirections;
			if(!empty($classdirections))
			foreach($classdirections as $path)
			{	
				$includePaths[] = $applicationRoot.'/'.$path;
			}
			$generator = new C_GeneratorAssembly($applicationRoot.'/'.$_zend_config->environment->autoload, $includePaths);
			$generator->generate();
		}
		include $applicationRoot.'/'.$_zend_config->environment->autoload;
		include $Z_ROOT.'/Global.php';
		$this->_zend_autoloader->registerNamespace('C_');
		$this->_zend_autoloader->registerNamespace('D_');
		$this->_zend_autoloader->registerNamespace('L_');
		$this->_zend_autoloader->registerNamespace('E_');
		$this->_zend_autoloader->registerNamespace('T_');
		$this->_zend_autoloader->pushAutoloader('Z_autoload');
		Zend_Registry::set("zend_config", $_zend_config);
		Zend_Registry::set("applicationRoot", $applicationRoot);
		//BeanFinder::register("config", $config);
		$error = new L_Error();
		return $this;
	}
	public function RunMvc()
	{
		C_Controller::getInstance()
		->setApplicationRoot($this->getApplicationRoot())
		->Run();
	}
}
?>
